
#include "uploadListHandle.h"
#include "cli_main.h"
LISTGROUD listGroud[2];

void initListGroud()
{
    I32 i = 0;
    for ( i = 0; i < MAX_LIST; i++ )
    {
        listGroud[DATA_TYPE_IMSI].list[i].uptime = 0;
        listGroud[DATA_TYPE_IMSI].list[i].startId = 0;      
        listGroud[DATA_TYPE_IMSI].list[i].endId = 0;      
        listGroud[DATA_TYPE_IMSI].list[i].timerId = (TIMER_ID_UPOADLIST1 + i);      
        listGroud[DATA_TYPE_IMSI].list[i].isWaittingRsp = false; 
        listGroud[DATA_TYPE_IMSI].list[i].isOldData = false;               
    }

    for ( i = 0; i < MAX_LIST; i++ )
    {
        listGroud[DATA_TYPE_MAC].list[i].uptime = 0;
        listGroud[DATA_TYPE_MAC].list[i].startId = 0;      
        listGroud[DATA_TYPE_MAC].list[i].endId = 0;      
        listGroud[DATA_TYPE_MAC].list[i].timerId = (TIMER_ID_MAC_UPOADLIST1 + i);      
        listGroud[DATA_TYPE_MAC].list[i].isWaittingRsp = false; 
        listGroud[DATA_TYPE_MAC].list[i].isOldData = false;               
    }
}


UPLOADLIST *getNotUsedList(I32 type)
{
    I32 i = 0;
    for ( i = 0; i < MAX_LIST; i++ )
    {
        if ( !listGroud[type].list[i].isWaittingRsp )
        {
            return &listGroud[type].list[i];
        }
        cli_app_log(CLI_LOG_INFO, "using  timd Id 0x%0x time is %d\n", listGroud[type].list[i].timerId, listGroud[type].list[i].uptime);
    }
    return NULL;
}

UPLOADLIST *oldDataGetNotUseList(I32 type)
{
    I32 i = 0, oldListNum = 0;
    for ( i = 0; i < MAX_LIST; i++ )
    {
        if ( listGroud[type].list[i].isWaittingRsp )
        {

            if ( !listGroud[type].list[i].isOldData )
            {
                return NULL;
            }
            oldListNum++;         
            cli_app_log(CLI_LOG_INFO, "old List num is %d timd Id 0x%0x\n", oldListNum, listGroud[type].list[i].timerId);  
            if ( oldListNum == MAX_OLD_LIST )
            {
                return NULL;
            }
        }
    }
    
    for ( i = 0; i < MAX_LIST; i++ )
    {
        if ( !listGroud[type].list[i].isWaittingRsp )
        {
            return &listGroud[type].list[i];
        }
    }
    return NULL; 
}

UPLOADLIST *searchListByTimeId(I32 timerId, I32 type)
{
    I32 i = 0;
    for ( i = 0; i < MAX_LIST; i++ )
    {
        if ( listGroud[type].list[i].timerId == timerId )
            return &listGroud[type].list[i];
    }
    return NULL;
}

UPLOADLIST *RestListByUptime(U32 uptime, I32 type)
{
    I32 i = 0;
    for ( i = 0; i < MAX_LIST; i++ )
    {
        if ( listGroud[type].list[i].uptime == uptime )
        {
            listGroud[type].list[i].isWaittingRsp = false;
            listGroud[type].list[i].isOldData = false;
            listGroud[type].list[i].uptime = 0;
            return &listGroud[type].list[i];
        }
    }
    return NULL;
}

void setListUptime(U32 *time, U32 *listUptime, I32 type)
{
    I32 i = 0;
    for ( i = 0; i < MAX_LIST; i++ )
    {
        if ( listGroud[DATA_TYPE_IMSI].list[i].uptime == *time )
        {
            (*time)++; 
            cli_app_log(CLI_LOG_WARNING, "uptime is same %d \n", listGroud[DATA_TYPE_IMSI].list[i].uptime);
            i = 0;
        }       
        if ( listGroud[DATA_TYPE_MAC].list[i].uptime == *time )
        {
            (*time)++; 
            cli_app_log(CLI_LOG_WARNING, "uptime is same %d \n", listGroud[DATA_TYPE_MAC].list[i].uptime);
            i = 0;
        }    
    }

    *listUptime = *time;
    cli_app_log(CLI_LOG_INFO, "set uptime is %d \n", *time);
}

bool noUsingList(I32 type)
{
    I32 i = 0;
    for ( i = 0; i < MAX_LIST; i++)
    {
        if ( listGroud[type].list[i].isWaittingRsp )
            return false;

    }
    return true;
}


